#ifndef SSHMONITOR_H
#define SSHMONITOR_H

#include <QObject>
#include <QProcess>
#include <QTimer>
#include <QString>
#include "datacollectionthread.h"

class SSHMonitor : public QObject
{
    Q_OBJECT

public:
    explicit SSHMonitor(QObject *parent = nullptr);
    ~SSHMonitor();

    // 简单的连接接口
    bool connectToHost(const QString &host, const QString &user);
    void disconnect();
    bool isConnected() const;

    // 获取远程CPU数据
    CPUData getRemoteCPUData() const;

signals:
    void dataUpdated(const CPUData &data);
    void connectionStatusChanged(bool connected);
    void errorOccurred(const QString &error);

private slots:
    void onSSHCommandFinished(int exitCode, QProcess::ExitStatus exitStatus);
    void onSSHCommandError(QProcess::ProcessError error);
    void updateRemoteData();

private:
    QString executeSSHCommand(const QString &command);
    CPUData parseCPUStats(const QString &data);
    void calculateCPUUsage(const QString &data);

    QString m_host;
    QString m_user;
    bool m_connected;

    QTimer *m_updateTimer;
    QProcess *m_sshProcess;

    // 用于计算CPU使用率的临时变量
    bool m_firstRead;
    unsigned long long m_lastUser;
    unsigned long long m_lastSystem;
    unsigned long long m_lastIdle;
    unsigned long long m_lastTotal;

    // 当前CPU数据
    CPUData m_currentData;
};

#endif // SSHMONITOR_H

